//
//  BMSnomHttpConnection.m
//  snomPhone
//
//  Created by Daniel Hazelbaker on 3/30/09.
//  Copyright 2009 Blue Box Moon. All rights reserved.
//

#import "BMSnomHttpConnection.h"
#import "HTTPResponse.h"
#import "Controller.h"


@implementation BMSnomHttpConnection

- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
    NSMutableDictionary	*parameters = nil;
    NSString		*command = nil;
    NSArray		*split1, *split2, *split3;
    int			i;


    NSLog(@"Web Request: %@", path);
    //
    // Split out the command and any parameters.
    //
    split1 = [path componentsSeparatedByString:@"?"];
    command = [[split1 objectAtIndex:0] substringFromIndex:1];
    if ([split1 count] > 1) {
	split2 = [[split1 objectAtIndex:1] componentsSeparatedByString:@"&"];
	parameters = [NSMutableDictionary dictionary];
	for (i = 0; i < [split2 count]; i++) {
	    split3 = [[split2 objectAtIndex:i] componentsSeparatedByString:@"="];
	    [parameters setObject:[[split3 objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] forKey:[split3 objectAtIndex:0]];
	}
    }

    //
    // Dispatch the request.
    //
    if ([command isEqual:@"incoming"] == YES)
	[[server delegate] processIncomingCall:parameters];
    else if ([command isEqual:@"outgoing"] == YES)
	[[server delegate] processOutgoingCall:parameters];
    else if ([command isEqual:@"connected"] == YES)
	[[server delegate] processConnectedCall:parameters];
    else if ([command isEqual:@"disconnected"] == YES)
	[[server delegate] processDisconnectedCall:parameters];
//    else
//	NSLog(@"Unknown command = %@, Parameters = %@", command, parameters);

    return [[[HTTPDataResponse alloc] initWithData:[NSData dataWithBytes:"\n" length:1]] autorelease];
}


@end
